package com.ruoyi.syf.controller;

import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.syf.domain.RiskAnalysisUnit;
import com.ruoyi.syf.domain.vo.RiskAnalysisUnitVo;
import com.ruoyi.syf.handler.deleteChain.DeleteService;
import com.ruoyi.syf.service.IRiskAnalysisUnitService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安全分析单元Controller
 *
 * @author yr
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/syf/unit")
public class RiskAnalysisUnitController extends BaseController {
    @Autowired
    private IRiskAnalysisUnitService riskAnalysisUnitService;
    @Autowired
    private DeleteService deleteService;

    /**
     * 查询安全分析单元列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RiskAnalysisUnitVo riskAnalysisUnit) {
        startPage();
        List<RiskAnalysisUnitVo> list = riskAnalysisUnitService.list(riskAnalysisUnit);
        return getDataTable(list);
    }

    /**
     * 获取安全分析单元详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(riskAnalysisUnitService.selectRiskAnalysisUnitById(id));
    }

    /**
     * 新增安全分析单元
     */
    @PreAuthorize("@ss.hasPermi('syf:unit:add')")
    @Log(title = "安全分析单元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RiskAnalysisUnit riskAnalysisUnit) {
        return toAjax(riskAnalysisUnitService.insertRiskAnalysisUnit(riskAnalysisUnit));
    }

    /**
     * 修改安全分析单元
     */
    @PreAuthorize("@ss.hasPermi('syf:unit:edit')")
    @Log(title = "安全分析单元", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RiskAnalysisUnit riskAnalysisUnit) {
        return toAjax(riskAnalysisUnitService.updateRiskAnalysisUnit(riskAnalysisUnit));
    }

    /**
     * 修改安全分析单元状态
     */
    @PreAuthorize("@ss.hasPermi('syf:unit:edit')")
    @Log(title = "安全分析单元", businessType = BusinessType.UPDATE)
    @PutMapping("/upStatus/{id}")
    public AjaxResult upStatus(@PathVariable("id") Long id, String reason) {
        return toAjax(riskAnalysisUnitService.upStatus(id, reason));
    }

    /**
     * 修改安全分析单元扫码状态
     */
    @PreAuthorize("@ss.hasPermi('syf:unit:edit')")
    @Log(title = "安全分析单元", businessType = BusinessType.UPDATE)
    @PutMapping("/upUnitScan/{id}")
    public AjaxResult upUnitScan(@PathVariable("id") Long id) {
        return toAjax(riskAnalysisUnitService.upUnitScan(id));
    }

    /**
     * 删除安全分析单元
     */
    @PreAuthorize("@ss.hasPermi('syf:unit:remove')")
    @Log(title = "安全分析单元", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        deleteService.deleteUnit(Arrays.asList(ids));
        return toAjax(true);
    }

    /**
     * 生成二维码
     */
    @GetMapping("/QRCode")
    public void QRCode(RiskAnalysisUnitVo vo, HttpServletResponse response) throws Exception {
        List<Long> ids;
        if ("0".equals(vo.getQRCodeType())){
            List<RiskAnalysisUnitVo> list = riskAnalysisUnitService.list(vo);
            ids = list.stream().map(RiskAnalysisUnitVo::getId).collect(Collectors.toList());
        }else {
            ids = Arrays.asList(vo.getUnitIds());
        }
        ServletOutputStream outputStream = response.getOutputStream();
        BufferedImage qrCodeImage = QRCodeUtil.createQrCode(vo.getQRCodeExplain(), String.valueOf(ids));
        response.setContentType("image/png");
        ImageIO.write(qrCodeImage, "png", outputStream);
    }

}
